<?php

/**
 * 购物车接口
 *
 * @author: yeyu <409166486@qq.com> 2016-11-11
 */
class Api_ShopCart extends PhalApi_Api
{

    public function getRules()
    {
        return array(
            'add' => array(
                "cookieUserId" => array(
                    "name" => "cookieUserId", "required" => true, "type" => "string", "desc" => "浏览器cookieID"),
                "productId" => array(
                    "name" => "productId", "required" => true, "type" => "int", "desc" => "產品編號"),
                "num" => array(
                    "name" => "num", "required" => true, "type" => "int", "desc" => "數量"),
                "color" => array(
                    "name" => "color", "type" => "string", "desc" => "顏色"),
                "size" => array(
                    "name" => "size", "type" => "string", "desc" => "尺碼")
            ),
            'getList' => array(
                "cookieUserId" => array(
                    "name" => "cookieUserId", "required" => true, "type" => "string", "desc" => "浏览器cookieID"),
                "orderSn" => array(
                    "name" => "orderSn", "required" => true, "type" => "string", "desc" => "訂單號，加入購物車成功之後會返回")
            ),
            'delete' => array(
                "cookieUserId" => array(
                    "name" => "cookieUserId", "required" => true, "type" => "string", "desc" => "浏览器cookieID"),
                "dbId" => array(
                    "name" => "id", "required" => true, "type" => "int", "desc" => "数据库购物车列表主键")
            ),
            'clear' => array(
                "cookieUserId" => array(
                    "name" => "cookieUserId", "required" => true, "type" => "string", "desc" => "浏览器cookieID")
            )
        );
    }


    /**
     * 物品添加到購物車接口
     * @return string orderSn 訂單號
     */
    public function add()
    {
        $rs = array('code' => 0, "msg" => "", "order_sn" => "");

        $domainSp = new Domain_ShopCart();

        $domainAutoSn = new Domain_Sn();
        $lastId = $domainAutoSn->lastedInsertId();
        $orderSn = "SN_" . $lastId;
        $data = array("userid" => $this->cookieUserId, "order_sn" => $orderSn, "product_id" => $this->productId, "size" => $this->size, "color" => $this->color, "product_nums" => $this->num, "createtime" => time());
        $ret = $domainSp->add($data);
        if ($ret) {
            $rs["code"] = 0;
            $rs["msg"] = "添加成功";
            $rs["order_sn"] = $ret['order_sn'];
        } else {
            $rs["code"] = -1;
            $rs["msg"] = "失敗";
            $rs["order_sn"] = "";
        }

        return $rs;
    }

    /**
     * 获取购物车物品列表接口
     * @return array list 商品信息（名称，数量，颜色，尺码，图片等）
     */
    public function getList()
    {
        $rs = array('list' => array());

        $domainSp = new Domain_ShopCart();

        $rs['list'] = $domainSp->getAll($this->cookieUserId, $this->orderSn);

        return $rs;
    }


    /**
     * 删除购物车物品接口
     * @return bool code 是否成功 （0：成功 -1：失败））
     */
    public function delete()
    {
        $rs = array('code' => 0, "msg" => "");

        $domainSp = new Domain_ShopCart();

        $ret = $domainSp->delete($this->cookieUserId, $this->dbId);
        if ($ret) {
            $rs["code"] = 0;
            $rs["msg"] = "刪除成功";
        } else {
            $rs["code"] = -1;
            $rs["msg"] = "失敗";
        }

        return $rs;
    }



    /**
     * 清空购物车所有物品接口
     * @return bool code 是否成功 （0：成功 -1：失败））
     */
    public function clear()
    {
        $rs = array('code' => 0, "msg" => "");

        $domainSp = new Domain_ShopCart();

        $ret = $domainSp->clear($this->cookieUserId);
        if ($ret) {
            $rs["code"] = 0;
            $rs["msg"] = "清空完成";
        } else {
            $rs["code"] = -1;
            $rs["msg"] = "失敗";
        }

        return $rs;
    }
}
